<!-- 账户设置 -->
<template>
    <div class="admin">
        <div class="editList">
            <div class="editListName">
                用户名称：
            </div>
            <div class="editListInpFont">
                {{adminName}}
            </div>
        </div>
        <div class="editList">
            <div class="editListName">
                原密码：
            </div>
            <input class="editListInpBox" v-model='codeOne' type="password" placeholder="8-16位数字字母组合" oninput="value=value.replace(/[^a-zA-Z0-9]/ig,'')"
                maxlength="16">
            <span class="warn" v-show='warnShowOne'>{{warnOneVal}}</span>
        </div>
        <div class="editList">
            <div class="editListName">
                新密码：
            </div>
            <input class="editListInpBox" v-model='codeTwo' type="password" placeholder="8-16位数字字母组合" oninput="value=value.replace(/[^a-zA-Z0-9]/ig,'')"
                maxlength="16">
            <span class="warn" v-show='warnShowTwo'>8-16位数字字母组合</span>
        </div>
        <div class="editList">
            <div class="editListName">
                确认新密码：
            </div>
            <input class="editListInpBox" v-model='codeThree' type="password" placeholder="8-16位数字字母组合" oninput="value=value.replace(/[^a-zA-Z0-9]/ig,'')"
                maxlength="16">
            <span class="warn" v-show='warnShowThree'>两次密码不一致，请重新输入</span>
        </div>
        <div class="addPassword" @click="addPasswordClick()">
            保 存
        </div>
    </div>
</template>
<script>
    import taskRequest from "../../assets/task";
    const crypto = require("crypto");
    export default {
        components: {},
        data() {
            return {
                adminName: "", //用户名
                codeOne: "", //原密码
                warnShowOne: false, //原密码判断
                warnOneVal: "原密码错误，请重新输入", //原密码判断
                codeTwo: "", //新密码
                warnShowTwo: false, //原密码判断
                codeThree: "", //确认新密码
                warnShowThree: false //新密码确认判断
            };
        },
        filters: {},
        created: function () {
            //权限
            if (sessionStorage.getItem('ADMIN')) {
                let obj = JSON.parse(sessionStorage.getItem('ADMIN'))
                this.adminName = obj.account
            } else {
                this.$router.push({
                    path: "/login"
                });
                sessionStorage.clear();
            }
        },
        watch: {
            //  $route: "fetchData"
        },
        methods: {
            //保存新密码
            addPasswordClick() {
                let self = this;
                let reg = new RegExp("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{8,16}$");
                if (self.codeOne.length < 8) {
                    self.warnOneVal = "8-16位数字字母组合";
                    self.warnShowOne = true;
                    return;
                } else {
                    if (reg.test(self.codeOne)) {
                        self.warnShowOne = false;
                    } else {
                        self.warnOneVal = "8-16位数字字母组合";
                        self.warnShowOne = true;
                        return;
                    }
                }
                if (self.codeTwo.length < 8) {
                    self.warnShowTwo = true;
                    return;
                } else {
                    if (reg.test(self.codeTwo)) {
                        self.warnShowTwo = false;
                    } else {
                        self.warnShowTwo = true;
                        return;
                    }
                }
                if (self.codeTwo != self.codeThree) {
                    self.warnShowThree = true;
                    return;
                } else {
                    self.warnShowThree = false;
                }
               
                let possible = self.codeOne;
                let shaCode = crypto.createHash("md5");
                let md5Str = shaCode.update(possible, "utf-8").digest("hex");
                let possibleNew = self.codeThree;
                let shaCodeNew = crypto.createHash("md5");
                let md5StrNew = shaCodeNew.update(possibleNew, "utf-8").digest("hex");
            
                taskRequest.adminSettingFun({
                    newPassword: md5StrNew,
                    oldPassword: md5Str
                }).then(data => {
                    if (data.statusCode == 200) {
                        self.$message({
                            showClose: true,
                            message: "修改成功",
                            type: "success"
                        });
                    } else {
                        self.$message(data.statusMessage)
                    }
                }).catch(msg => {
                    self.$message('服务器异常')
                })


            }
        }
    };
</script>
<style scoped>
    .admin {
        height: 100%;
        padding-top: 97px;
        animation: listCenterBox 0.5s;
    }

    .addPassword {
        width: 300px;
        height: 50px;
        background: linear-gradient(-135deg, #3296e0, #4ddaff);
        border-radius: 30px;
        font-size: 18px;
        text-align: center;
        line-height: 50px;
        color: white;
        cursor: pointer;
        margin-top: 70px;
        margin-left: 320px;
    }
</style>